#!/bin/bash
file=''
if [[ $# < 1 ]]; then
    basedir=`cd $(dirname $0); pwd -P`
    name=`basename $0 .sh`
    file="${basedir}/../../../data/ecs/${name}.txt"
else
    OPTS=`getopt -o f:0:1:2:3:4:5:6:7:8: --long file:,CommandContent:,Name:,RegionId:,Type:,Description:,EnableParameter:,SourceRegionId:,Timeout:,WorkingDir: -n 'parse-options' -- "$@"`
    if [[ $? != 0 ]] ; then
        echo "Failed parsing options." >&2 ;
    exit 1 ;
    fi
    eval set -- "${OPTS}"

    CommandContent=''
    Name=''
    RegionId=''
    Type=''
    Description=''
    EnableParameter=''
    SourceRegionId=''
    Timeout=''
    WorkingDir=''

    while true; do
        case "$1" in
            -f | --file ) file="$2"; shift ;;
            -0 | --CommandContent ) CommandContent="$2"; shift; shift ;;
            -1 | --Name ) Name="$2"; shift; shift ;;
            -2 | --RegionId ) RegionId="$2"; shift; shift ;;
            -3 | --Type ) Type="$2"; shift; shift ;;
            -4 | --Description ) Description="$2"; shift; shift ;;
            -5 | --EnableParameter ) EnableParameter="$2"; shift; shift ;;
            -6 | --SourceRegionId ) SourceRegionId="$2"; shift; shift ;;
            -7 | --Timeout ) Timeout="$2"; shift; shift ;;
            -8 | --WorkingDir ) WorkingDir="$2"; shift; shift ;;
            -- ) shift; break ;;
            * ) break ;;
        esac
    done

    if [[ -z ${file} ]];then
        echo CommandContent=${CommandContent}
        echo Name=${Name}
        echo RegionId=${RegionId}
        echo Type=${Type}
        echo Description=${Description}
        echo EnableParameter=${EnableParameter}
        echo SourceRegionId=${SourceRegionId}
        echo Timeout=${Timeout}
        echo WorkingDir=${WorkingDir}

        args=""
        if [[  -n ${CommandContent}  ]];then
            args="${args} --CommandContent ${CommandContent}"
        fi
        if [[  -n ${Name}  ]];then
            args="${args} --Name ${Name}"
        fi
        if [[  -n ${RegionId}  ]];then
            args="${args} --RegionId ${RegionId}"
        fi
        if [[  -n ${Type}  ]];then
            args="${args} --Type ${Type}"
        fi
        if [[  -n ${Description}  ]];then
            args="${args} --Description ${Description}"
        fi
        if [[  -n ${EnableParameter}  ]];then
            args="${args} --EnableParameter ${EnableParameter}"
        fi
        if [[  -n ${SourceRegionId}  ]];then
            args="${args} --SourceRegionId ${SourceRegionId}"
        fi
        if [[  -n ${Timeout}  ]];then
            args="${args} --Timeout ${Timeout}"
        fi
        if [[  -n ${WorkingDir}  ]];then
            args="${args} --WorkingDir ${WorkingDir}"
        fi
        echo ${args}
        response=`aliyun ecs CreateCommand ${args}`
        echo ${response}
    fi
fi

if [[ -n ${file} ]];then
    if [[ ! -f ${file} ]];then
    echo "the $file is not a file"
    exit 2
    fi
    declare -A paramsData=()
    while read line
    do
    if [[ ${#line} -gt 1 ]];then
    IFS='=' read -ra param <<< "$line"
    key=${param[0]}
    value=${param[1]}
    paramsData["${key}"]=${value}
    fi
    done <${file}
    args=""
    if [[  -n ${paramsData["CommandContent"]} ]];then
    args="${args} --CommandContent ${paramsData["CommandContent"]}"
    fi
    if [[  -n ${paramsData["Name"]} ]];then
    args="${args} --Name ${paramsData["Name"]}"
    fi
    if [[  -n ${paramsData["RegionId"]} ]];then
    args="${args} --RegionId ${paramsData["RegionId"]}"
    fi
    if [[  -n ${paramsData["Type"]} ]];then
    args="${args} --Type ${paramsData["Type"]}"
    fi
    if [[  -n ${paramsData["Description"]} ]];then
    args="${args} --Description ${paramsData["Description"]}"
    fi
    if [[  -n ${paramsData["EnableParameter"]} ]];then
    args="${args} --EnableParameter ${paramsData["EnableParameter"]}"
    fi
    if [[  -n ${paramsData["SourceRegionId"]} ]];then
    args="${args} --SourceRegionId ${paramsData["SourceRegionId"]}"
    fi
    if [[  -n ${paramsData["Timeout"]} ]];then
    args="${args} --Timeout ${paramsData["Timeout"]}"
    fi
    if [[  -n ${paramsData["WorkingDir"]} ]];then
    args="${args} --WorkingDir ${paramsData["WorkingDir"]}"
    fi
    echo ${args}
    response=`aliyun ecs CreateCommand ${args}`
    echo ${response}
fi
